package kr.hyosang.android.penfo.parser;

import kr.hyosang.android.penfo.SharedObject;
import kr.hyosang.android.penfo.data.Article;
import kr.hyosang.android.penfo.data.ArticleViewData;
import kr.hyosang.android.penfo.data.Comment;
import android.util.Log;

public class FreeBoardParser extends Parser<ArticleViewData> {
	@Override
	public ArticleViewData parse(String html) {
		ArticleViewData data = new ArticleViewData();
		
		String divider;
		int nIdx;
		
		SharedObject.getInstance().bLogin = isLogin(html);
		
		String mainData = getSubContent(html, "css/penfo_light_gray/bg_view_title.gif", "<tr align=center valign=middle class='title'");
		
		if(mainData.length() > 0) {
			//Content
			Article article = new Article();
			
			String contentArea = mainData;
			
			String subject = getSubContent(contentArea, "<font class=view_title2>", "</font>");
			String category = getSubContent(contentArea, "<font class=view_cate>", "</font>");
			String writer = getSubContent(contentArea, "<font class=view_name>", "</font>");
			String regdate = getSubContent(contentArea, "등록일:", "<br>");
			String hitcount = getSubContent(contentArea, "조회수:", "<revolution");
			String profile = getSubContent(contentArea, "<td valign=top class=han width=100 align=right><img src=", " ");
			
			article.subject = subject;
			article.category = category;
			article.writer = writer;
			article.regdate = regdate;
			article.hitcount = hitcount;
			if(profile.length() > 0 && !profile.startsWith("/")) {
				profile = "/bbs/" + profile;
			}
			article.profileUrl = profile;
			
			
			divider = "<table border=0 cellspacing=0 cellpadding=0 width=98% class=pic_bg>";
			nIdx = 0;
			
			String tmpArea = getSubContent(contentArea, divider, divider);
			int nContentIdx = nEndIdx;
			
			//첫번째 영역은 첨부파일 영역
			while(true) {
				String attach = getSubContent(tmpArea, "<img src=\"", "\"", nIdx);
				if(attach.length() > 0) {
					if(!attach.startsWith("/")) {
						attach = "/bbs/"+attach;
					}
					article.addAttachment(attach);
					nIdx = nEndIdx;
				}else {
					break;
				}
			}
			
			contentArea = getSubContent(contentArea, "<td align=left valign=top class=han style=line-height:165%>", "</td></tr></table>", nContentIdx);
			article.htmlContent = contentArea;
			
			String commentArea = getSubContent(html, "<td width=8><img src=skin/DQ_Revolution_BBS/t.gif width=8 height=1></td>", "<tr><td class=lined style=height:1px><img src=skin/DQ_Revolution_BBS/t.gif height=1></td></tr>");
			divider = "<table border=0 width=98% cellspacing=0 cellpadding=0 class=info_bg style=table-layout:fixed>";
			
			String singleComment = getSubContent(commentArea, divider, divider);
			
			if(nStartIdx != -1 && nEndIdx == -1) {   //코멘트가 하나만 있는 경우
			    nEndIdx = commentArea.length();
			    singleComment = commentArea.substring(nStartIdx);
			}
			nIdx = nEndIdx;
			
			while(singleComment.length() > 0) {
				String datetime = getSubContent(singleComment, "<font class=eng>", "</font>");
				
				Comment c = new Comment();
				c.writerName = getSubContent(singleComment, "<font class=view_name>", "</font>");
				c.comment = getSubContent(singleComment, "style=\"line-height:160%\">", "</div>");
				c.writeDate = getSubContent(datetime, "", "<br>");
				c.writeTime = getSubContent(datetime, "<br>");
				
				article.addComment(c);
				
				singleComment = getSubContent(commentArea, divider, "<tr><td height=10 class=info_bg></td></tr>", nIdx);
				nIdx = nEndIdx;
				if(nStartIdx != -1 && nEndIdx == -1) {
					//맨 마지막꺼
					singleComment = getSubContent(commentArea, divider, "", nStartIdx-divider.length());
					nIdx = commentArea.length();
				}
				
				Log.d("penfo", c.comment);
			}
			
			data.thisArticle = article;
		}
		
		//리스트부분 파싱
		divider = "<tr><td colspan=11 class=line_shadow height=3></td></tr>";
		String listPart = getSubContent(html, divider, "<span id=\"copyright\">");
		String noticeArea = getSubContent(listPart, divider);
		if(noticeArea.length() > 0) {
			
		}
		
		divider = "<tr align=\"center\" class=\"list";
		
		nIdx = listPart.indexOf(divider);
		
		while(nIdx >= 0) {
			String line = getSubContent(listPart, divider, divider, nIdx);
			if(nStartIdx > 0 && nEndIdx == -1) {
				//last.
				line = getSubContent(listPart, divider, "", nIdx);
			}
			nIdx = nEndIdx;
			
			Article a = new Article();
			
			a.subject = getSubContent(line, "<font class=list_title>", "</font>");
			a.category = getSubContent(line, "<td class='han list_vspace' nowrap colspan=2><nobr>", "</nobr>");
			a.writer = getSubContent(line, "<span class=list_name>", "</span>");
			a.no = getSubContent(line, "&no=", "\"");
			a.regdate = getSubContent(line, "<td nowrap class='eng list_vspace' colspan=2><nobr>", "</nobr>");
			a.hitcount = getSubContent(line, "<td nowrap class='eng list_vspace' colspan=2><nobr>", "</nobr>", nEndIdx);
			
			data.addListArticle(a);
			
			Log.d("penfo", "subject : " + a);
		}

		return data;
	}
}
